package com.gaojiawei.multiThread16_JUCUtil;

import java.util.concurrent.Semaphore;

public class L03_Semaphore {
    public static void main(String[] args) throws InterruptedException {
        Semaphore semaphore = new Semaphore(10);

        new Thread(()->{
            System.out.println("1,2,3 are ready");
            try {
                semaphore.acquire(3);
                System.out.println("1,2,3 go");
                Thread.sleep(10000);
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                System.out.println("1,2,3 finish");
                semaphore.release(3);
            }
        }).start();


        for(int i = 0; i<7;i++){
            new Thread(()->{
                System.out.println("again are ready");
                try {
                    semaphore.acquire(3);
                    System.out.println("again go");
                    Thread.sleep(1000);
                } catch (Exception e) {
                    e.printStackTrace();
                }finally {
                    System.out.println("again finish");
                    semaphore.release(1);
                }
            }).start();
        }

        Thread.sleep(10000);


        new Thread(()->{
            System.out.println("one,two,three are ready");
            try {
                semaphore.acquire(3);
                System.out.println("one two three go");
                Thread.sleep(10000);
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                System.out.println("one two three finish");
                semaphore.release(3);
            }
        }).start();


        for(int i = 0; i<7;i++){
            new Thread(()->{
                System.out.println("again++ are ready");
                try {
                    semaphore.acquire(3);
                    System.out.println("again++ go");
                    Thread.sleep(1000);
                } catch (Exception e) {
                    e.printStackTrace();
                }finally {
                    System.out.println("again++ finish");
                    semaphore.release(1);
                }
            }).start();
        }


    }
}
